package com.peng.leetcode.sort;

import java.util.Arrays;

/**
 * LargestNumber
 * 给定一组非负整数 nums，重新排列它们每个数字的顺序（每个数字不可拆分）使之组成一个最大的整数。
 *
 * @author: lupeng6
 * @create: 2020/12/11 10:21
 */
public class LargestNumber {

    public static void main(String[] args) {
        int[] arr = {10, 2};
        String s = new Solution().largestNumber(arr);
        System.out.println(s);
    }

    static class Solution {
        public String largestNumber(int[] nums) {
            String[] strArr = new String[nums.length];
            for (int i = 0; i < nums.length; i++) {
                strArr[i] = String.valueOf(nums[i]);
            }

            Arrays.sort(strArr, (a, b) -> {
                String s1 = a + "";
                String s2 = b + "";
                return s2.compareTo(s1);
            });

            if (strArr[0].equals("0")) {
                return "0";
            }

            StringBuilder builder = new StringBuilder();
            for (String s : strArr) {
                builder.append(s);
            }
            return builder.toString();
        }
    }

}
